package com.xg.java.util;

import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class FileZipUtils {
    public void zipFile(ZipOutputStream zipOutputStream, File file, String parentFileName){
        FileInputStream in = null;
        try {
            ZipEntry zipEntry = new ZipEntry( parentFileName+file.getName() );
            zipOutputStream.putNextEntry( zipEntry );
            in = new FileInputStream( file);
            int len;
            byte [] buf = new byte[8*1024];
            while ((len = in.read(buf)) != -1){
                zipOutputStream.write(buf, 0, len);
            }
            zipOutputStream.closeEntry(  );
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try{
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 递归压缩目录结构
     * @param zipOutputStream
     * @param file
     * @param parentFileName
     */
    public  void directory(ZipOutputStream zipOutputStream,File file,String parentFileName){
        File[] files = file.listFiles();
        String parentFileNameTemp = null;
        for (File fileTemp:
                files) {
            parentFileNameTemp =  StringUtils.isEmpty(parentFileName)?fileTemp.getName():parentFileName+"/"+fileTemp.getName();
            if(fileTemp.isDirectory()){
                directory(zipOutputStream,fileTemp, parentFileNameTemp);
            }else{
                zipFile(zipOutputStream,fileTemp,parentFileNameTemp);
            }
        }
    }

    /**
     * 压缩文件目录
     * @param source 源文件目录（单个文件和多层目录）
     * @param destit 目标目录
     */
    public  void zipFiles(String source,String destit) {

        File file = new File( source );
        ZipOutputStream zipOutputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream( destit );
            zipOutputStream = new ZipOutputStream( fileOutputStream );
            if (file.isDirectory()) {
                directory( zipOutputStream, file, "" );
            } else {
                zipFile( zipOutputStream, file, "" );
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                zipOutputStream.close();
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    public  void copyDir1(String sourcePathDir, String newPathDir) throws IOException {
        File start = new File(sourcePathDir);
        File end = new File(newPathDir);
        String[] filePath = start.list();//获取该文件夹下的所有文件以及目录的名字
        if(!end.exists()) {
            end.mkdir();
        }
        boolean flag = false;
        for(String temp : filePath) {
            //添加满足情况的条件
            if ((new File(sourcePathDir + File.separator + temp)).isDirectory()) {
                copyDir1(sourcePathDir + File.separator + temp, newPathDir + File.separator + temp);
                flag = true;
            }
            if(new File(sourcePathDir + File.separator + temp ).isFile()) {
                //为文件则进行拷贝
                flag = copyFile1(sourcePathDir + File.separator + temp, newPathDir + File.separator+temp );
            }
            if(flag){
                System.out.println("文件:" + temp + ",复制成功！");
            }else{
                System.out.println("文件:" + temp + ",复制失败！");
            }
        }
    }

    //文件的拷贝
    public static boolean copyFile1(String sourcePath, String newPath) {
        boolean flag = false;
        File readfile = new File(sourcePath);
        File newFile = new File(newPath);
        BufferedWriter bufferedWriter = null;
        Writer writer = null;
        FileOutputStream fileOutputStream = null;
        BufferedReader bufferedReader = null;
        try{
            fileOutputStream = new FileOutputStream(newFile, true);
            writer = new OutputStreamWriter(fileOutputStream,"UTF-8");
            bufferedWriter = new BufferedWriter(writer);

            bufferedReader = new BufferedReader(new FileReader(readfile));

            String line = null;
            while((line = bufferedReader.readLine()) != null){
                bufferedWriter.write(line);
                bufferedWriter.newLine();
                bufferedWriter.flush();
            }
            flag = true;
        } catch(IOException e) {
            flag = false;
            e.printStackTrace();
        } finally {
            try {
                if(bufferedWriter != null){
                    bufferedWriter.close();
                }
                if(bufferedReader != null){
                    bufferedReader.close();
                }
                if(writer != null){
                    writer.close();
                }
                if(fileOutputStream != null){
                    fileOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return flag;
    }
}
